package com.apes.scm.masterdata.product.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.Tools;
import com.apes.scm.masterdata.product.model.AssetClassification;
import com.apes.scm.masterdata.product.repository.AssetClassificationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static java.util.stream.Collectors.toMap;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.scm.masterdata.product.service
 * @ClassName: AssetClassificationService
 * @Author: pyb
 * @Description: 资产分类服务
 * @CreateTime: 2021-04-22 11:48
 * @Version: 1.0
 */
@Service("assetClassificationService")
public class AssetClassificationService extends DomainService {
    @Autowired
    AssetClassificationRepository assetClassificationRepository;

    /**
     * 保存
     *
     * @param request
     * @return
     */
    public AssetClassification save(SimpleRequest request) {
        AssetClassification assetClassification = request.getO(AssetClassification.class);
        assetClassification.setLevels(Tools.isNull(assetClassification.getParents()) ? 1 : assetClassification.getParents().getLevels() + 1);
        return assetClassificationRepository.saveAndFlush(assetClassification);
    }

    /**
     * 删除
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        AssetClassification assetClassification = request.getO(AssetClassification.class);
        assetClassificationRepository.delete(assetClassification.getId());
        return "删除成功";
    }

    /**
     * 查询所有
     * @param request
     * @return
     */
    public List<AssetClassification> findAllForTree(SimpleRequest request) {
        List<AssetClassification> classificationList = assetClassificationRepository.findAll();
        return classificationList;
    }

}
